<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="类别" prop="type">
      <el-input v-model="dataForm.type" placeholder="类别"></el-input>
    </el-form-item>
    <el-form-item label="过磅单ID" prop="weighDetailId">
      <el-input v-model="dataForm.weighDetailId" placeholder="过磅单ID"></el-input>
    </el-form-item>
    <el-form-item label="过磅单编号" prop="weighDetailNo">
      <el-input v-model="dataForm.weighDetailNo" placeholder="过磅单编号"></el-input>
    </el-form-item>
    <el-form-item label="计划单ID" prop="planOrderId">
      <el-input v-model="dataForm.planOrderId" placeholder="计划单ID"></el-input>
    </el-form-item>
    <el-form-item label="计划单编号" prop="planOrderNo">
      <el-input v-model="dataForm.planOrderNo" placeholder="计划单编号"></el-input>
    </el-form-item>
    <el-form-item label="单据时间" prop="dayTime">
      <el-input v-model="dataForm.dayTime" placeholder="单据时间"></el-input>
    </el-form-item>
    <el-form-item label="原料名称ID" prop="ingredientId">
      <el-input v-model="dataForm.ingredientId" placeholder="原料名称ID"></el-input>
    </el-form-item>
    <el-form-item label="原料名称" prop="ingredientName">
      <el-input v-model="dataForm.ingredientName" placeholder="原料名称"></el-input>
    </el-form-item>
    <el-form-item label="单价" prop="unitPrice">
      <el-input v-model="dataForm.unitPrice" placeholder="单价"></el-input>
    </el-form-item>
    <el-form-item label="金额" prop="totalPrice">
      <el-input v-model="dataForm.totalPrice" placeholder="金额"></el-input>
    </el-form-item>
    <el-form-item label="折扣" prop="discount">
      <el-input v-model="dataForm.discount" placeholder="折扣"></el-input>
    </el-form-item>
    <el-form-item label="折扣单价" prop="discountUnitPrice">
      <el-input v-model="dataForm.discountUnitPrice" placeholder="折扣单价"></el-input>
    </el-form-item>
    <el-form-item label="期初金额" prop="openingBalanceAmount">
      <el-input v-model="dataForm.openingBalanceAmount" placeholder="期初金额"></el-input>
    </el-form-item>
    <el-form-item label="应付金额" prop="balancePayAmount">
      <el-input v-model="dataForm.balancePayAmount" placeholder="应付金额"></el-input>
    </el-form-item>
    <el-form-item label="财务收款ID" prop="financePayId">
      <el-input v-model="dataForm.financePayId" placeholder="财务收款ID"></el-input>
    </el-form-item>
    <el-form-item label="供应商ID" prop="supplierId">
      <el-input v-model="dataForm.supplierId" placeholder="供应商ID"></el-input>
    </el-form-item>
    <el-form-item label="供应商名称" prop="supplierName">
      <el-input v-model="dataForm.supplierName" placeholder="供应商名称"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    <el-form-item label="附加说明" prop="additionalRemark">
      <el-input v-model="dataForm. v" placeholder="附加说明"></el-input>
    </el-form-item>
    <el-form-item label="净重" prop="realNetWeight">
      <el-input v-model="dataForm.realNetWeight" placeholder="净重"></el-input>
    </el-form-item>
    <el-form-item label="运费" prop="freightAmountPrice">
      <el-input v-model="dataForm.freightAmountPrice" placeholder="运费"></el-input>
    </el-form-item>
    <el-form-item label="运费结算方式" prop="freightSettlementStatus">
      <el-input v-model="dataForm.freightSettlementStatus" placeholder="运费结算方式"></el-input>
    </el-form-item>
    <el-form-item label="余额" prop="remainingSum">
      <el-input v-model="dataForm.remainingSum" placeholder="余额"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态"></el-input>
    </el-form-item>
    <el-form-item label="" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="createUser">
      <el-input v-model="dataForm.createUser" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="updateUser">
      <el-input v-model="dataForm.updateUser" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          type: '',
          weighDetailId: '',
          weighDetailNo: '',
          planOrderId: '',
          planOrderNo: '',
          dayTime: '',
          ingredientId: '',
          ingredientName: '',
          unitPrice: '',
          totalPrice: '',
          discount: '',
          discountUnitPrice: '',
          openingBalanceAmount: '',
          balancePayAmount: '',
          financePayId: '',
          supplierId: '',
          supplierName: '',
          remark: '',
          additionalRemark: '',
          realNetWeight: '',
          freightAmountPrice: '',
          freightSettlementStatus: '',
          remainingSum: '',
          status: '',
          createTime: '',
          createUser: '',
          updateTime: '',
          updateUser: ''
        },
        dataRule: {
          type: [
            { required: true, message: '类别不能为空', trigger: 'blur' }
          ],
          weighDetailId: [
            { required: true, message: '过磅单ID不能为空', trigger: 'blur' }
          ],
          weighDetailNo: [
            { required: true, message: '过磅单编号不能为空', trigger: 'blur' }
          ],
          planOrderId: [
            { required: true, message: '计划单ID不能为空', trigger: 'blur' }
          ],
          planOrderNo: [
            { required: true, message: '计划单编号不能为空', trigger: 'blur' }
          ],
          dayTime: [
            { required: true, message: '单据时间不能为空', trigger: 'blur' }
          ],
          ingredientId: [
            { required: true, message: '原料名称ID不能为空', trigger: 'blur' }
          ],
          ingredientName: [
            { required: true, message: '原料名称不能为空', trigger: 'blur' }
          ],
          unitPrice: [
            { required: true, message: '单价不能为空', trigger: 'blur' }
          ],
          totalPrice: [
            { required: true, message: '金额不能为空', trigger: 'blur' }
          ],
          discount: [
            { required: true, message: '折扣不能为空', trigger: 'blur' }
          ],
          discountUnitPrice: [
            { required: true, message: '折扣单价不能为空', trigger: 'blur' }
          ],
          openingBalanceAmount: [
            { required: true, message: '期初金额不能为空', trigger: 'blur' }
          ],
          balancePayAmount: [
            { required: true, message: '应付金额不能为空', trigger: 'blur' }
          ],
          financePayId: [
            { required: true, message: '财务收款ID不能为空', trigger: 'blur' }
          ],
          supplierId: [
            { required: true, message: '供应商ID不能为空', trigger: 'blur' }
          ],
          supplierName: [
            { required: true, message: '供应商名称不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ],
          additionalRemark: [
            { required: true, message: '附加说明不能为空', trigger: 'blur' }
          ],
          realNetWeight: [
            { required: true, message: '净重不能为空', trigger: 'blur' }
          ],
          freightAmountPrice: [
            { required: true, message: '运费不能为空', trigger: 'blur' }
          ],
          freightSettlementStatus: [
            { required: true, message: '运费结算方式不能为空', trigger: 'blur' }
          ],
          remainingSum: [
            { required: true, message: '余额不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          createUser: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          updateUser: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/finance/financetransactioningredient/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.type = data.financeTransactionIngredient.type
                this.dataForm.weighDetailId = data.financeTransactionIngredient.weighDetailId
                this.dataForm.weighDetailNo = data.financeTransactionIngredient.weighDetailNo
                this.dataForm.planOrderId = data.financeTransactionIngredient.planOrderId
                this.dataForm.planOrderNo = data.financeTransactionIngredient.planOrderNo
                this.dataForm.dayTime = data.financeTransactionIngredient.dayTime
                this.dataForm.ingredientId = data.financeTransactionIngredient.ingredientId
                this.dataForm.ingredientName = data.financeTransactionIngredient.ingredientName
                this.dataForm.unitPrice = data.financeTransactionIngredient.unitPrice
                this.dataForm.totalPrice = data.financeTransactionIngredient.totalPrice
                this.dataForm.discount = data.financeTransactionIngredient.discount
                this.dataForm.discountUnitPrice = data.financeTransactionIngredient.discountUnitPrice
                this.dataForm.openingBalanceAmount = data.financeTransactionIngredient.openingBalanceAmount
                this.dataForm.balancePayAmount = data.financeTransactionIngredient.balancePayAmount
                this.dataForm.financePayId = data.financeTransactionIngredient.financePayId
                this.dataForm.supplierId = data.financeTransactionIngredient.supplierId
                this.dataForm.supplierName = data.financeTransactionIngredient.supplierName
                this.dataForm.remark = data.financeTransactionIngredient.remark
                this.dataForm.additionalRemark = data.financeTransactionIngredient.additionalRemark
                this.dataForm.realNetWeight = data.financeTransactionIngredient.realNetWeight
                this.dataForm.freightAmountPrice = data.financeTransactionIngredient.freightAmountPrice
                this.dataForm.freightSettlementStatus = data.financeTransactionIngredient.freightSettlementStatus
                this.dataForm.remainingSum = data.financeTransactionIngredient.remainingSum
                this.dataForm.status = data.financeTransactionIngredient.status
                this.dataForm.createTime = data.financeTransactionIngredient.createTime
                this.dataForm.createUser = data.financeTransactionIngredient.createUser
                this.dataForm.updateTime = data.financeTransactionIngredient.updateTime
                this.dataForm.updateUser = data.financeTransactionIngredient.updateUser
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/finance/financetransactioningredient/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'type': this.dataForm.type,
                'weighDetailId': this.dataForm.weighDetailId,
                'weighDetailNo': this.dataForm.weighDetailNo,
                'planOrderId': this.dataForm.planOrderId,
                'planOrderNo': this.dataForm.planOrderNo,
                'dayTime': this.dataForm.dayTime,
                'ingredientId': this.dataForm.ingredientId,
                'ingredientName': this.dataForm.ingredientName,
                'unitPrice': this.dataForm.unitPrice,
                'totalPrice': this.dataForm.totalPrice,
                'discount': this.dataForm.discount,
                'discountUnitPrice': this.dataForm.discountUnitPrice,
                'openingBalanceAmount': this.dataForm.openingBalanceAmount,
                'balancePayAmount': this.dataForm.balancePayAmount,
                'financePayId': this.dataForm.financePayId,
                'supplierId': this.dataForm.supplierId,
                'supplierName': this.dataForm.supplierName,
                'remark': this.dataForm.remark,
                'additionalRemark': this.dataForm.additionalRemark,
                'realNetWeight': this.dataForm.realNetWeight,
                'freightAmountPrice': this.dataForm.freightAmountPrice,
                'freightSettlementStatus': this.dataForm.freightSettlementStatus,
                'remainingSum': this.dataForm.remainingSum,
                'status': this.dataForm.status,
                'createTime': this.dataForm.createTime,
                'createUser': this.dataForm.createUser,
                'updateTime': this.dataForm.updateTime,
                'updateUser': this.dataForm.updateUser
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
